/***************************************************************************//**
 * @file CGI/SceneRoot.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2013 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef SCENE_ROOT_CPP
#define SCENE_ROOT_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <osgUtil/Optimizer>

#include <CGI/SceneRoot.h>

////////////////////////////////////////////////////////////////////////////////

SceneRoot::SceneRoot( CGI_Data *data ) :
    m_data ( data ),

    m_hazeScene ( 0 ),
    m_skyDome   ( 0 )
{
    m_sceneRoot = new osg::Group();
    m_sceneRoot->setName( "SceneRoot" );

    m_sceneRoot->addChild( createSceneRootGroup().get() );
}

////////////////////////////////////////////////////////////////////////////////

SceneRoot::~SceneRoot()
{
    m_sceneRoot->removeChildren( 0, m_sceneRoot->getNumChildren() );

    if ( m_hazeScene ) delete m_hazeScene; m_hazeScene = 0;
    if ( m_skyDome   ) delete m_skyDome;   m_skyDome   = 0;
}

////////////////////////////////////////////////////////////////////////////////

void SceneRoot::reset()
{
    m_sceneRoot->removeChildren( 0, m_sceneRoot->getNumChildren() );

    if ( m_hazeScene ) delete m_hazeScene; m_hazeScene = 0;
    if ( m_skyDome   ) delete m_skyDome;   m_skyDome   = 0;

    m_sceneRoot->addChild( createSceneRootGroup().get() );

    osgUtil::Optimizer optimzer;
    optimzer.optimize( m_sceneRoot.get() );
}

////////////////////////////////////////////////////////////////////////////////

void SceneRoot::update()
{
    //////////////////////////////////////////
    if ( m_hazeScene ) m_hazeScene ->update();
    if ( m_skyDome   ) m_skyDome   ->update();
    //////////////////////////////////////////
}

////////////////////////////////////////////////////////////////////////////////

osg::ref_ptr<osg::Group> SceneRoot::createSceneRootGroup()
{
    osg::ref_ptr<osg::Group> sceneRootGroup = new osg::Group();
    sceneRootGroup->setName( "SceneRootGroup" );

    osg::ref_ptr<osg::StateSet> sceneRootStateSet = sceneRootGroup->getOrCreateStateSet();

    sceneRootStateSet->setMode( GL_RESCALE_NORMAL , osg::StateAttribute::ON );
    sceneRootStateSet->setMode( GL_LIGHT0         , osg::StateAttribute::ON );

    m_hazeScene = new HazeScene ( m_data );
    m_skyDome   = new SkyDome   ( m_data );

    sceneRootGroup->addChild( m_hazeScene ->getHazeSceneGroup() .get() );
    sceneRootGroup->addChild( m_skyDome   ->getSkyDomeGroup()   .get() );

    return sceneRootGroup;
}
